﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_Dazzle : TargetAreaSpell
    {
        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, false, out MapItemsWithin12) == false)
                return;

            MapItem targetsMapItem = null;
            List<Character> CharactersInUpdateRange = new List<Character>();
            foreach (MapItem mapitem in MapItemsWithin12)
            {
                if (mapitem.MapX == TargetCreatureBase.MapX && mapitem.MapY == TargetCreatureBase.MapY)
                    targetsMapItem = mapitem;

                foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                {
                    if (creaturebase is Character)
                        CharactersInUpdateRange.Add((Character)creaturebase);
                }
            }

            targetsMapItem.SpellLayer0.Add(this);
            MapItems.Add(targetsMapItem);

            //Build a list of creatures first
            List<CreatureBase> creaturesToDamage = new List<CreatureBase>();
            foreach (CreatureBase creaturebase in targetsMapItem.CreaturesBases)
            {
                if (creaturebase.HP > 0 &&
                    (creaturebase.Faction == TargetCreatureBase.Faction || myCreatureBase.hateManager.GetCreatureHateInfo(creaturebase) != null) && 
                    creaturebase.CanSeeMe(myCreatureBase))
                    creaturesToDamage.Add(creaturebase);
            }

            List<CreatureBase> creatureBasesToUpdate = new List<CreatureBase>();

            //Damage them later, damaging them can cause creatures to be removed from the tile which will break the above iteration
            foreach (CreatureBase creaturebase in creaturesToDamage)
            {
                if (creaturebase.HP > 0 && creaturebase.Faction == TargetCreatureBase.Faction)
                {
                    //If we're attacking a player, and we dont have a flag on them, give them a flag on us.
                    if (creaturebase is Character &&
                        myCreatureBase is Character &&
                        creaturebase != myCreatureBase &&
                        myCreatureBase.hateManager.GetCreatureHateInfo(creaturebase) == null &&
                        spellIcon.Offensive == true)
                    {
                        Character charToAttack = (Character)creaturebase;
                        if (charToAttack.friendsManager.hasNoSDFlag(CastingCharacterID) == false)
                        {
                            if (creaturebase.hateManager.GetCreatureHateInfo(myCreatureBase) == null)
                            {
                                creaturebase.SendMessage("You have been ATTACKED by " + myCreatureBase.Name + "!", "damaged");
                                creaturebase.PlaySound("Ding", creaturebase.MapX, creaturebase.MapY);

                                creaturebase.hateManager.UpdateList(myCreatureBase, 0);


                                charToAttack.AddMapItem(myCreatureBase.currentMapItem);
                            }
                            else
                                creaturebase.hateManager.UpdateList(myCreatureBase, 0);
                        }
                    }

                    Spell_Mesmerize buff = new Spell_Mesmerize();
                    if (buff.tryResist(this.spellIcon, CastingCreatureLevel, CastingCreatureInt, CastingCreatureWis, creaturebase) == true)
                    {
                        if (creaturebase != myCreatureBase)
                            myCreatureBase.SendMessage(creaturebase.Name + " resists " + spellIcon.Name + "!", "spelldefense");

                        creaturebase.SendMessage("You resist " + myCreatureBase.Name + "'s " + spellIcon.Name + "!", "spelloffense");
                    }
                    else
                    {
                        Buff oldDazzle = creaturebase.buffManager.GetBuffByStackType("Mesmerize");
                        if (oldDazzle != null)
                            oldDazzle.RemoveSpell(false, false);

                        CreatureBase cb = creaturebase;
                        buff.Resume(cb, this.spellIcon, -1, CastLevel, CastingCharacterID, CastingCreatureInt, CastingCreatureWis, -1, null);
                        cb.buffManager.UpdateBuffs();

                        if (creaturebase != myCreatureBase)
                            myCreatureBase.SendMessage(creaturebase.Name + " is mesmerized!", "spelldefense");

                        creaturebase.SendMessage("You are mesmerized by " + myCreatureBase.Name + "'s " + spellIcon.Name + "!", "spelloffense");

                        creatureBasesToUpdate.Add(creaturebase);
                    }
                }
            }
            
            foreach (Character character in CharactersInUpdateRange)
            {
                //character.PlaySound("Lightening", targetsMapItem.MapX, targetsMapItem.MapY);
                character.AddMapItem(targetsMapItem);

                foreach (CreatureBase cb in creatureBasesToUpdate)
                    character.AddCreatureBase(cb);
            }
        }
    }
}
